package com.myapi.client;

import java.util.ArrayList;

import com.extjs.gxt.ui.client.widget.ContentPanel;
import com.extjs.gxt.ui.client.widget.LayoutContainer;
import com.extjs.gxt.ui.client.widget.form.FieldSet;
import com.extjs.gxt.ui.client.widget.layout.FitData;
import com.extjs.gxt.ui.client.widget.layout.FitLayout;
import com.google.gwt.core.client.GWT;
import com.google.gwt.user.client.Random;
import com.google.gwt.user.client.Timer;
import com.google.gwt.user.client.rpc.AsyncCallback;
import com.myapi.client.DAO.LoginDAO;
import com.myapi.client.DAO.PlayerDAO;
import com.myapi.client.DAO.StatisticDAO;
import com.extjs.gxt.charts.client.Chart;
import com.extjs.gxt.charts.client.model.ChartModel;
import com.extjs.gxt.charts.client.model.Legend;
import com.extjs.gxt.charts.client.model.Legend.Position;
import com.extjs.gxt.charts.client.model.axis.XAxis;
import com.extjs.gxt.charts.client.model.charts.*;

public class StatisticUI  extends ContentPanel{

	/**
	 * The message displayed to the user when the server cannot be reached or
	 * returns an error.
	 */
	private static final String SERVER_ERROR = "An error occurred while "
		+ "attempting to contact the server. Please check your network "
		+ "connection and try again.";

	/**
	 * Create a remote service xy to talk to the server-side Greeting service.
	 */
	private final StatisticServiceAsync statisticService = GWT
	.create(StatisticService.class);

	LoginDAO loginDAO;
	StatisticDAO statisticDAO;
	public StatisticUI(LoginDAO loginDAO) {
		this.loginDAO=loginDAO;
		setHeading("Pie chart");  
		setFrame(true);  
		setSize(400, 400);  
		setLayout(new FitLayout());  



		PlayerDAO player =new PlayerDAO();
		player.setId(loginDAO.getEmailAddress());
		statisticDAO=new StatisticDAO();
		statisticDAO.setPlayer(player);





	}
	public void getStatistic(){
		statisticService.getStatistic( statisticDAO.getPlayer(), new AsyncCallback< ArrayList<PlayerHandDAO>>(){

			@Override
			public void onFailure(Throwable caught) {
				// TODO Auto-generated method stub

			}

			@Override
			public void onSuccess( ArrayList<PlayerHandDAO> playerHandDAO) {
				StatisticDAO stat=new StatisticDAO();
				stat.setListHand(playerHandDAO);
				setStatistic(stat);
				System.out.println("setStatistic"); 
			}


		});
	}
	public void setStatistic(StatisticDAO statisticDAO){
		String url;    
		url = "resources/chart/open-flash-chart.swf";  
		Chart chart = new Chart(url);  
		chart.setBorders(true);
		chart.setChartModel(getChipsChart(statisticDAO));  

		add(chart);   
		this.recalculate();
	}
	private ChartModel getChipsChart(StatisticDAO statisticDAO){
		ChartModel cm = new ChartModel("Chips Evolution Player:"+statisticDAO.getPlayer().getName(),  
		"font-size: 14px; font-family: Verdana;");  
		cm.setBackgroundColour("#ffffff");  
		XAxis xa = new XAxis();  
		cm.setXAxis(xa);  
		AreaChart area1 = new AreaChart();  
		area1.setFillAlpha(0.3f);  
		area1.setColour("#ff0000");  
		area1.setFillColour("#ff0000");  
		int chips= 60;
		area1.addValues(60);
		for (int n = 0; n < statisticDAO.getListHand().size(); n++) {  
			chips+= statisticDAO.getListHand().get(n).chipsDiff;
			area1.addValues(chips);
		}  
		cm.addChartConfig(area1);
		return cm;  

	}  

	private ChartModel getPieChartData() {  
		ChartModel cm = new ChartModel("Sales by Region",  
		"font-size: 14px; font-family: Verdana; text-align: center;"); 
		cm.setBackgroundColour("#fffff5");  
		Legend lg = new Legend(Position.RIGHT, true);  
		lg.setPadding(10);  
		cm.setLegend(lg);  

		PieChart pie = new PieChart();  
		pie.setAlpha(0.5f);  
		pie.setNoLabels(true);  
		pie.setTooltip("#label# $#val#M<br>#percent#");  
		pie.setColours("#ff0000", "#00aa00", "#0000ff", "#ff9900", "#ff00ff");  
		pie.addSlices(new PieChart.Slice(100, "AU","Australia"));  
		pie.addSlices(new PieChart.Slice(200, "US", "USA"));  
		pie.addSlices(new PieChart.Slice(150, "JP", "Japan"));  
		pie.addSlices(new PieChart.Slice(120, "DE", "Germany"));  
		pie.addSlices(new PieChart.Slice(60, "UK", "United Kingdom"));  

		cm.addChartConfig(pie);  
		return cm;  
	}  

	private LayoutContainer getChart() {  
		FieldSet fs = new FieldSet();  
		fs.setHeading("Chart");  
		fs.setLayout(new FitLayout());  

		String url= "ressources/chart/open-flash-chart.swf";  

		final Chart chart = new Chart(url);  
		chart.setBorders(true);  
		fs.add(chart, new FitData(0, 0, 20, 0));  

		return fs;  
	}  


}
